<template>
  <div>
    <h1>停车列表</h1>
    <!-- 条件查询 -->
    <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
      <el-form-item label="车牌号">
        <el-input
          v-model="searchFrom.keyWord"
          placeholder="请输入车牌号"
          clearable
        />
      </el-form-item>
      <el-form-item label="车牌号">
        <el-date-picker
          v-model="searchFrom.beginTime"
          type="datetime"
          placeholder="请选择"
        />
        --
        <el-date-picker
          v-model="searchFrom.endTime"
          type="datetime"
          placeholder="请选择"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loadCarList">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-button type="success" @click="onAddCar()">车辆入场</el-button>
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column prop="carId" label="车位编号" width="100" />
      <el-table-column prop="carCode" label="车牌号" width="160" />
      <el-table-column prop="upTime" label="入场时间" width="180" />
      <el-table-column prop="timing" label="计时标准" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success" @click="showCar(scope.row)"
            >查看费用明细</el-button
          >
          <el-button type="primary" @click="payCar(scope.row)"
            >车辆离场</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="margin-top: 20px">
      <el-pagination
        v-model:current-page="searchFrom.pageNum"
        v-model:page-size="searchFrom.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 车辆入场 -->
    <div>
      <el-dialog v-model="dialogFormVisible" title="车辆入场" width="500">
        <el-form :model="form">
          <el-form-item label="车牌号" :label-width="formLabelWidth">
            <el-input v-model="form.carCode" autocomplete="off" />
          </el-form-item>
          <el-form-item label="剩余车位" :label-width="formLabelWidth">
            <el-input v-model="form.parking" autocomplete="off" readonly />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="saveCar()"> 提交 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!-- 查看费用明细 -->
    <div>
      <el-dialog v-model="dialogShowFormVisible" title="车辆入场" width="500">
        <el-form :model="showForm">
          <el-form-item label="车牌号" :label-width="formLabelWidth">
            <el-input v-model="showForm.carCode" autocomplete="off" readonly />
          </el-form-item>
          <el-form-item label="入场时间" :label-width="formLabelWidth">
            <el-input v-model="showForm.upTime" autocomplete="off" readonly />
          </el-form-item>
          <el-form-item label="停车时长" :label-width="formLabelWidth">
            <el-input v-model="showForm.timing" autocomplete="off" />
          </el-form-item>
          <el-form-item label="停车费用" :label-width="formLabelWidth">
            <el-input v-model="showForm.price" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogShowFormVisible = false">取消</el-button>
            <el-button type="primary" @click="editCar()"> 自主结算 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <!-- 离场 -->
    <div>
      <el-dialog v-model="dialogPayFormVisible" title="车辆离场" width="500">
        <el-form :model="payForm">
          <el-form-item label="车牌号" :label-width="formLabelWidth">
            <el-input v-model="payForm.carCode" autocomplete="off" readonly />
          </el-form-item>

          <el-form-item label="缴费状态" :label-width="formLabelWidth">
            <el-radio-group v-model="payForm.status">
              <el-radio :value="1" size="large">未缴费</el-radio>
              <el-radio :value="2" size="large">已缴费</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogPayFormVisible = false">取消</el-button>
            <el-button type="primary" @click="editPayCar()"> 提交 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getCarList, addCar, updateCar } from "@/api/api";
import type { RefSymbol } from "@vue/reactivity";
import { ElMessage, ElSubMenu } from "element-plus";
import { fa } from "element-plus/es/locales.mjs";
const tableData = ref([]);
const total = ref(0);
const dialogFormVisible = ref(false);
const dialogShowFormVisible = ref(false);
const dialogPayFormVisible = ref(false);
const formLabelWidth = "140px";
const showForm = ref({
  carCode: "",
  parking: 0,
  price: 0,
  status: 0,
  timing: "",
  upTime: "",
});
const payForm = ref({
  carCode: "",
  parking: 0,
  price: 0,
  status: 0,
  timing: "",
  upTime: "",
});
const form = ref({
  carCode: null,
  parking: 500,
});
const searchFrom = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  beginTime: null,
  endTime: null,
});

//离场
const editPayCar = () => {
  updateCar(payForm.value).then((res) => {
    if (payForm.value.status == 1) {
      ElMessage.warning("请先完成缴费");
    } else if (res.data.code == 200) {
      ElMessage.success("缴费成功,愿君一路顺风！");
      dialogPayFormVisible.value = false;
      loadCarList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
//离场弹框
const payCar = (row: any) => {
  Object.assign(payForm.value, row);
  dialogPayFormVisible.value = true;
};

//修改
const editCar = () => {
  updateCar(showForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("结算成功");
      dialogShowFormVisible.value = false;
      loadCarList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const showCar = (row: any) => {
  Object.assign(showForm.value, row);
  dialogShowFormVisible.value = true;
};
//车辆入场
const saveCar = () => {
  addCar(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("入场成功");
      form.value.parking -= 1;
      dialogFormVisible.value = false;
      loadCarList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//展示车辆入场弹框
const onAddCar = () => {
  form.value.carCode = null;
  dialogFormVisible.value = true;
};

//加载列表数据
const loadCarList = () => {
  getCarList(searchFrom.value).then((res) => {
    tableData.value = res.data.data.records;
    total.value = res.data.data.total;
  });
};
//每页条数

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchFrom.value.pageSize = val;
  loadCarList();
};
//当前页数
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchFrom.value.pageNum = val;
  loadCarList();
};

//初始化数据
onMounted(() => {
  loadCarList();
});
</script>
